package com.edais.actions;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.edais.service.ShopVouchersService;
import com.edais.util.CommonUtil;
import com.edais.util.Consts;
import com.edais.util.JsonUtil;
import com.edais.util.Message;
import com.edais.util.StringUtil;

@Controller
@RequestMapping("/shopVochers")

public class ShopVouchersAction {
	private static final Log logger = LogFactory.getLog(ShopVouchersAction.class);
	
	@Autowired
	private ShopVouchersService shopVouchersService;
	
	/**
	 * 我的兑换券
	 * @param request
	 * @param response
	 */
	@RequestMapping(value = "/addShopVouchers")
	public void addShopVouchers(HttpServletRequest request,
			HttpServletResponse response) {
		Map<String, String> jsonMap = new HashMap<String, String>();
		// 解密请求参数
		Map<String, String> paramsMap = CommonUtil.decryptParamters(request);
		String userId = paramsMap.get("userId");//用户ID
		String mobile = paramsMap.get("mobile");//用户手机号
		String voucherId = paramsMap.get("voucherId");//兑换券ID
		String source = paramsMap.get("source");//来源
		
//		userId = "112233";
//		mobile="18616132001";
//		voucherId="1";
//		source="2";
		
		try{
			if (StringUtil.isEmpty(userId) ||StringUtil.isEmpty(mobile)
					|| StringUtil.isEmpty(voucherId) || StringUtil.isEmpty(source)) {
				String result = CommonUtil.setResultStringCn(new HashMap<String, Object>(),
					Consts.ERROR_CODE, Consts.ErrorMsg.MSG00001,Message.CnDescriptionExpression.BUSINESSE_ERROR);

				// 处理返回结果
				CommonUtil.responseJson(result, response);
				logger.error("shopVochers/addShopVouchers 传入用户ID或手机号或兑换券ID或来源为空");
				return;
			}
			Map<String,Object> reqMap = new HashMap<String,Object>();
			reqMap.put("userId", userId);
			reqMap.put("mobile", mobile);
			reqMap.put("voucherId",voucherId);
			reqMap.put("source",source);
			jsonMap = this.shopVouchersService.addShopVouchers(reqMap);
			// 处理返回结果
			String result = JsonUtil.getJsonStringFromMap(jsonMap);
			CommonUtil.responseJson(result, response);
		}catch (Exception e) {
			logger.error("userId:"+userId + "获取电商兑换券异常:" + e);
			String result = CommonUtil.setResultStringCn(
				new HashMap<String, Object>(),Consts.ERROR_CODE, "",Message.CnDescriptionExpression.BUSINESSE_ERROR);
			// 处理返回结果
			CommonUtil.responseJson(result, response);
			return;
		}
	}
	
	/**
	 * 查询用户兑换券列表
	 * @param request
	 * @param response
	 */
	@RequestMapping(value = "/queryShopVouchers")
	public void queryShopVochers(HttpServletRequest request,
			HttpServletResponse response) {
		Map<String, Object> jsonMap = new HashMap<String, Object>();
		
		// 默认页
		Integer current = 1;
		Integer pageSize = 10;
		
		// 解密请求参数
		Map<String, String> paramsMap = CommonUtil.decryptParamters(request);
		String userId = paramsMap.get("userId");//用户ID
		String flag = paramsMap.get("flag");//flag=0：未使用的兑换券；flag=1：已使用的兑换券；flag=2：过期的兑换券；flag=3：已使用的和过期的兑换券
		String currentStr = paramsMap.get(Consts.Page.PAGE_CURRENT_NAME);   //当前页数
		String pageSizeStr = paramsMap.get(Consts.Page.PAGE_SIZE_NAME);     //页大小
		try{
			if (StringUtil.isEmpty(userId)) {
				String result = CommonUtil.setResultStringCn(new HashMap<String, Object>(),
					Consts.ERROR_CODE, Consts.ErrorMsg.MSG00001,Message.CnDescriptionExpression.BUSINESSE_ERROR);

				// 处理返回结果
				CommonUtil.responseJson(result, response);
				logger.error("shopVochers/queryShopVochers 传入用户ID为空");
				return;
			}
			if(null != currentStr && !("".equals(currentStr))){
				current = Integer.parseInt(currentStr);
			}
			if(null != pageSizeStr && !("".equals(pageSizeStr))){
				pageSize = Integer.parseInt(pageSizeStr);
			}
			//分页参数
			Map<String,Object> reqMap = new HashMap<String,Object>();
			reqMap.put("userId", userId);
			reqMap.put("flag", flag);
			reqMap.put("rowBegin",(current-1)*pageSize);
			reqMap.put("rowEnd",pageSize);
			
			jsonMap = this.shopVouchersService.queryShopVochers(reqMap);
			// 处理返回结果
			String result = JsonUtil.getJsonStringFromMap(jsonMap);
			CommonUtil.responseJson(result, response);
		}catch (Exception e) {
			logger.error("userId" +userId+ "查询用户兑换券异常:" + e);
			String result = CommonUtil.setResultStringCn(
				new HashMap<String, Object>(),Consts.ERROR_CODE, "",Message.CnDescriptionExpression.BUSINESSE_ERROR);
			// 处理返回结果
			CommonUtil.responseJson(result, response);
			return;
		}
	}
	
	/**
	 * 跳转电商一元购平台
	 * @param request
	 * @param response
	 */
	@RequestMapping(value = "/goUseShopVouchers")
	public void goUseShopVouchers(HttpServletRequest request,
			HttpServletResponse response) {
		Map<String, Object> jsonMap = new HashMap<String, Object>();
		// 解密请求参数
		Map<String, String> paramsMap = CommonUtil.decryptParamters(request);
		String mobile = paramsMap.get("mobile");//用户ID

		try{
			if (StringUtil.isEmpty(mobile)) {
				String result = CommonUtil.setResultStringCn(new HashMap<String, Object>(),
					Consts.ERROR_CODE, Consts.ErrorMsg.MSG00001,Message.CnDescriptionExpression.BUSINESSE_ERROR);

				// 处理返回结果
				CommonUtil.responseJson(result, response);
				logger.error("shopVochers/queryShopVochers 传入用户手机号为空");
				return;
			}
			
			jsonMap = this.shopVouchersService.goUseShopVouchers(mobile);
			// 处理返回结果
			String result = JsonUtil.getJsonStringFromMap(jsonMap);
			CommonUtil.responseJson(result, response);
		}catch (Exception e) {
			logger.error("mobile" +mobile+ "跳转电商一元购平台异常:" + e);
			String result = CommonUtil.setResultStringCn(
				new HashMap<String, Object>(),Consts.ERROR_CODE, "",Message.CnDescriptionExpression.BUSINESSE_ERROR);
			// 处理返回结果
			CommonUtil.responseJsonShop(result, response);
			return;
		}
	}
	
	/**
	 * 查询用户兑换券数量（电商）
	 * @param request
	 * @param response
	 */
	@RequestMapping(value = "/queryVouCountToShop")
	public void queryVouCountToShop(HttpServletRequest request,
			HttpServletResponse response) {
		Map<String, Object> jsonMap = new HashMap<String, Object>();
		// 解密请求参数
		Map<String, String> paramsMap = CommonUtil.decryptParamtersShop(request);
		String mobile = paramsMap.get("mobile");//用户手机号
//		String voucherType = paramsMap.get("voucherType");//兑换券类型
		
		try{
			if (StringUtil.isEmpty(mobile)) {
				String result = CommonUtil.setResultStringCn(new HashMap<String, Object>(),
					Consts.ERROR_CODE, Consts.ErrorMsg.MSG00001,Message.CnDescriptionExpression.BUSINESSE_ERROR);

				// 处理返回结果
				CommonUtil.responseJson(result, response);
				logger.error("shopVochers/queryVouCountToShop 传入用户手机号为空");
				return;
			}

			//参数
			Map<String,Object> reqMap = new HashMap<String,Object>();
			reqMap.put("mobile", mobile);
//			reqMap.put("voucherType", voucherType);
			
			jsonMap = this.shopVouchersService.queryVouCountToShop(reqMap);
			// 处理返回结果
			String result = JsonUtil.getJsonStringFromMap(jsonMap);
			CommonUtil.responseJsonShop(result, response);
		}catch (Exception e) {
			logger.error("mobile:" +mobile + "查询用户兑换券数量（电商）异常:" + e);
			String result = CommonUtil.setResultStringCn(
				new HashMap<String, Object>(),Consts.ERROR_CODE, "",e.getMessage());
			// 处理返回结果
			CommonUtil.responseJsonShop(result, response);
			return;
		}
	}
	
	/**
	 * 更新兑换券使用状态（电商）
	 * @param request
	 * @param response
	 */
	@RequestMapping(value = "/updateVouchersToShop")
	public void updateVouchersToShop(HttpServletRequest request,
			HttpServletResponse response) {
		Map<String, Object> jsonMap = new HashMap<String, Object>();
		// 解密请求参数
		Map<String, String> paramsMap = CommonUtil.decryptParamtersShop(request);
		String mobile = paramsMap.get("mobile");//用户手机号
		String voucherType = paramsMap.get("voucherType");//兑换券类型
		String vouCount = paramsMap.get("vouCount");//兑换券数量
		String orderId = paramsMap.get("orderId");//订单号
		
//		mobile="13856582637";
//		voucherType = "2";
//		vouCount = "2";
//		orderId="10208";
		
		try{
			if (StringUtil.isEmpty(mobile) || StringUtil.isEmpty(vouCount) 
					|| StringUtil.isEmpty(orderId) || StringUtil.isEmpty(voucherType)) {
				String result = CommonUtil.setResultStringCn(new HashMap<String, Object>(),
					Consts.ERROR_CODE, Consts.ErrorMsg.MSG00001,Message.CnDescriptionExpression.BUSINESSE_ERROR);

				// 处理返回结果
				CommonUtil.responseJson(result, response);
				logger.error("shopVochers/updateShopVouchers 传入手机号或兑换券类型或兑换券数量或订单号为空");
				return;
			}
			// 判断参数兑换券金额类型是否正确
			if (voucherType.matches("\\d+\\.\\d+$") && Double.valueOf(voucherType) > Double.valueOf(voucherType.substring(0, voucherType.indexOf(".")))) {
				String result = CommonUtil.setResultStringCn(new HashMap<String, Object>(),
						Consts.ERROR_CODE, Consts.ErrorMsg.MSG00001,Message.CnDescriptionExpression.BUSINESSE_ERROR);

				// 处理返回结果
				CommonUtil.responseJson(result, response);
				logger.error("shopVochers/updateShopVouchers 传入兑换券金额类型有误！voucherType:"+voucherType);
				return;
			}
			if (voucherType.matches("\\d+\\.\\d+$")) {
				voucherType = voucherType.substring(0, voucherType.indexOf("."));
			}
			Map<String,Object> reqMap = new HashMap<String,Object>();
			reqMap.put("mobile", mobile);
			reqMap.put("voucherType", voucherType);
			reqMap.put("vouCount", Integer.parseInt(vouCount)*Integer.parseInt(voucherType));
			reqMap.put("orderId", orderId);
			
			jsonMap = this.shopVouchersService.updateShopVochers(reqMap);
			// 处理返回结果
			String result = JsonUtil.getJsonStringFromMap(jsonMap);
			CommonUtil.responseJsonShop(result, response);
		}catch (Exception e) {
			logger.error("mobile:" + mobile + "更新用户兑换券（电商）异常:" + e);
			String result = CommonUtil.setResultStringCn(
				new HashMap<String, Object>(),Consts.ERROR_CODE, "",e.getMessage());
			// 处理返回结果
			CommonUtil.responseJsonShop(result, response);
			return;
		}
	}
	
	/**
	 * 根据参数查询用户已使用兑换券数量（电商）
	 * @param request
	 * @param response
	 */
	@RequestMapping(value = "/queryVouchersToShop")
	public void queryVouchersToShop(HttpServletRequest request,
			HttpServletResponse response) {
		Map<String, Object> jsonMap = new HashMap<String, Object>();
		// 解密请求参数
		Map<String, String> paramsMap = CommonUtil.decryptParamtersShop(request);
//		String buyTime = paramsMap.get("buyTime");//购买日期
		String orderId = paramsMap.get("orderId");//订单号
//		
//		buyTime="2016-10-14";
//		orderId = "00000002";
		
		try{
			if (StringUtil.isEmpty(orderId)) {
				String result = CommonUtil.setResultStringCn(new HashMap<String, Object>(),
					Consts.ERROR_CODE, Consts.ErrorMsg.MSG00001,Message.CnDescriptionExpression.BUSINESSE_ERROR);

				// 处理返回结果
				CommonUtil.responseJson(result, response);
				logger.error("shopVochers/queryVouchersToShop 传入订单号为空"+orderId);
				return;
			}

			//参数
			Map<String,Object> reqMap = new HashMap<String,Object>();
//			reqMap.put("buyTime", buyTime);
			reqMap.put("orderId", orderId);
			
			jsonMap = this.shopVouchersService.queryVouchersToShop(reqMap);
			// 处理返回结果
			String result = JsonUtil.getJsonStringFromMap(jsonMap);
			CommonUtil.responseJsonShop(result, response);
		}catch (Exception e) {
			logger.error("orderId:" +orderId + "根据参数查询用户已使用兑换券数量（电商）异常:" + e);
			String result = CommonUtil.setResultStringCn(
				new HashMap<String, Object>(),Consts.ERROR_CODE, "",e.getMessage());
			// 处理返回结果
			CommonUtil.responseJsonShop(result, response);
			return;
		}
	}
	
}
